﻿// -----------------------------------------------------------------------
// <copyright file="UIUpdater.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Yottamud_2013_v3_Main
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Yottamud_Project_V3;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Media;
    using Yottamud_2013_v3_Main.Game_Files;
    using System.Collections.ObjectModel;
using System.Windows.Input;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class UIUpdater
    {

        MainWindow _inputGUI;
        private SolidColorBrush combatOn = new SolidColorBrush(Colors.LimeGreen);
        private SolidColorBrush combatOff = new SolidColorBrush(Colors.Red);
        ObservableCollection<ShopEntry> _ShopCollection = new ObservableCollection<ShopEntry>();

        public ObservableCollection<ShopEntry> ShopCollection { get { return _ShopCollection; } }

        public void DisplayShopItems(int shopID)
        {

            ResourceTypes.ShopType shop = GameResources.FindShop(shopID);
            if (shop != null)
            {
                _inputGUI.Dispatcher.Invoke((Action)delegate()
                    {
                        _ShopCollection.Clear();
                        foreach (ResourceTypes.ItemType item in shop.ItemList)
                        {

                            _ShopCollection.Add(new ShopEntry { ShopItem = item.Name, ShopItemPrice = (item.Price + Math.Round(((decimal)item.Price * (shop.Markup / 100)))).ToString() });

                        }
                    });
            }
        }

        public UIUpdater(MainWindow inputGUI)
        {
            _inputGUI = inputGUI;
            SetupShopListView();
            SetupEquippedListView();
            SetupUnequippedListView();
            SetupKeysListView();
            SetupMonsterHistoryView();

        }

        public class NpcEntry
        {
            public string NpcName { get; set; }
            public int NpcExp { get; set; }
            public int NpcNumber { get; set; }
        }

        public class ItemEntry
        {
            public string ItemName {get;set;}
            public string ItemSlot { get; set; }
            public Button ItemButton { get; set; }
        }

        public class ShopEntry
        {
            private string _shopItemPrice { get; set; }
            public string ShopItem { get; set; }
            public string ShopItemPrice { get { return _shopItemPrice; } set { _shopItemPrice = value + " gold pieces"; } }
        }

     

        public void UpdateRestStatus(bool iStatus)
        {
            _inputGUI.Dispatcher.Invoke((Action)delegate()
               {
                   if (iStatus)
                   {
                       _inputGUI.restIcon.Visibility = System.Windows.Visibility.Visible;
                   }
                   else
                   {
                       _inputGUI.restIcon.Visibility = System.Windows.Visibility.Hidden;
                   }
               });

        }

        public void UpdateCombatStatus(bool iStatus)
        {
            if (iStatus)
            {
                _inputGUI.Dispatcher.Invoke((Action)delegate()
                {
                    _inputGUI.combatStatus.Fill = combatOn;
                });
            }
            else
            {
                _inputGUI.Dispatcher.Invoke((Action)delegate()
                {
                    _inputGUI.combatStatus.Fill = combatOff;
                });
            }

        }
        public void UpdateRoom(string iData)
        {
            _inputGUI.Dispatcher.Invoke((Action)delegate()
           {
               _inputGUI.debugRoom.Text = iData;
           });

        }

        public void UpdateCombat(string iData)
        {
          
        }

        public void UpdateCurrentHitpoints(GameDataDecoder.StatLineType iStat)
        {
            _inputGUI.Dispatcher.Invoke((Action)delegate()
            {
                _inputGUI.healthBar.Value = iStat.CurrentHitPoints;
            });

        }

        public void UpdateMaximumHP(GameDataDecoder.StatLineType iStat)
        {
            _inputGUI.Dispatcher.Invoke((Action)delegate()
            {
                _inputGUI.healthBar.Maximum = iStat.MaxHitPoints;
                _inputGUI.healthBar.Value = iStat.CurrentHitPoints;
            });
        }

        public void UpdateCommunications(GameDataDecoder.CommunicationType iChat)
        {
            switch (iChat.ChatDataType)
            {
                case GameDataDecoder.ChatType.Telepaths:
                    _inputGUI.Dispatcher.Invoke((Action)delegate()
                    {
                        _inputGUI.rtbTelepath.AppendText("[" + iChat.TimeStamp + "]: " + iChat.Player + 
                            " : " + iChat.Data + "\r\n");

                    });

                    break;
                case GameDataDecoder.ChatType.Gossips:
                    _inputGUI.Dispatcher.Invoke((Action)delegate()
                    {
                        _inputGUI.rtbGossip.AppendText("[" + iChat.TimeStamp + "]: " + iChat.Player + 
                            " : " + iChat.Data + "\r\n");
                    });

                    break;
            }

        }

        private void AddToKeyList(string item)
        {
            ItemEntry itemEnt = new ItemEntry();

            itemEnt.ItemName = item;
            itemEnt.ItemSlot = "Key";

            _inputGUI.lvKeys.Items.Add(itemEnt);

        }

        public void AddToMonsterList(string name, int exp, int number)
        {
            NpcEntry npcEnt = new NpcEntry();

            npcEnt.NpcNumber = number;
            npcEnt.NpcName = name;
            npcEnt.NpcExp = exp;

            _inputGUI.Dispatcher.Invoke((Action)delegate()
           {

               foreach (NpcEntry lvi in _inputGUI.lvNpcList.Items)
               {

                   if (lvi.NpcNumber == number)
                   {
                       return;
                   }

               }

               _inputGUI.lvNpcList.Items.Add(npcEnt);

           });

        }

        private void AddToInventoryList(string item, string slot)
        {
          
            ItemEntry itemEnt = new ItemEntry();
            
            itemEnt.ItemName = item;
            itemEnt.ItemSlot = slot;


            _inputGUI.lvWornItems.Items.Add(itemEnt);
        }

        public void GeneralDebug(string data)
        {

      

        }

        private void AddToUnEquippedList(string item)
        {
            ItemEntry itemEnt = new ItemEntry();

            itemEnt.ItemName = item;
            itemEnt.ItemSlot = "Not Equipped";

            _inputGUI.lvUnequippedItems.Items.Add(itemEnt);

        }

        private void SetupShopListView()
        {
            _inputGUI.lvShopItems.ItemsSource = ShopCollection;
            _inputGUI.lvShopItems.Focus();
        }

        private void SetupEquippedListView()
        {
            _inputGUI.lvWornItems.Items.Clear();

            GridView view = new GridView();
            GridViewColumn itemNameCol = new GridViewColumn();
            GridViewColumn slotCol = new GridViewColumn();

            slotCol.Header = "Slot";
            itemNameCol.Header = "Item";

            itemNameCol.DisplayMemberBinding = new Binding("ItemName");
            slotCol.DisplayMemberBinding = new Binding("ItemSlot");

            view.Columns.Add(itemNameCol);
            view.Columns.Add(slotCol);

            _inputGUI.lvWornItems.View = view;

        }

        private void SetupKeysListView()
        {
            _inputGUI.lvKeys.Items.Clear();

            GridView view = new GridView();
            GridViewColumn itemNameCol = new GridViewColumn();
            GridViewColumn slotCol = new GridViewColumn();

            slotCol.Header = "Slot";
            itemNameCol.Header = "Item";

            itemNameCol.DisplayMemberBinding = new Binding("ItemName");
            slotCol.DisplayMemberBinding = new Binding("ItemSlot");

            view.Columns.Add(itemNameCol);
            view.Columns.Add(slotCol);

            _inputGUI.lvKeys.View = view;
        }

        private void SetupMonsterHistoryView()
        {
            _inputGUI.lvNpcList.Items.Clear();


            GridView view = new GridView();
            GridViewColumn npcNameCol = new GridViewColumn();
            GridViewColumn npcExpCol = new GridViewColumn();

            npcExpCol.Header = "Exp";
            npcNameCol.Header = "Name";

            npcNameCol.DisplayMemberBinding = new Binding("NpcName");
            npcExpCol.DisplayMemberBinding = new Binding("NpcExp");

            view.Columns.Add(npcExpCol);
            view.Columns.Add(npcNameCol);

            _inputGUI.lvNpcList.View = view;

        }

        private void SetupUnequippedListView()
        {
            _inputGUI.lvUnequippedItems.Items.Clear();

            GridView view = new GridView();
            GridViewColumn itemNameCol = new GridViewColumn();
            GridViewColumn slotCol = new GridViewColumn();

            slotCol.Header = "Slot";
            itemNameCol.Header = "Item";

            itemNameCol.DisplayMemberBinding = new Binding("ItemName");
            slotCol.DisplayMemberBinding = new Binding("ItemSlot");

            view.Columns.Add(itemNameCol);
            view.Columns.Add(slotCol);
           
            _inputGUI.lvUnequippedItems.View = view;

        }


        public void UpdatePlayerInventory(InventoryManager.InventoryType iInventory)
        {
            _inputGUI.Dispatcher.Invoke((Action)delegate()
            {
                _inputGUI.lvUnequippedItems.Items.Clear();
                _inputGUI.lvKeys.Items.Clear();
                _inputGUI.lvWornItems.Items.Clear();

                AddToInventoryList(iInventory.PlayerWornItems.Head, "Head");
                AddToInventoryList(iInventory.PlayerWornItems.Eyes, "Eyes");
                AddToInventoryList(iInventory.PlayerWornItems.MainWeapon, "Main Weapon");
                AddToInventoryList(iInventory.PlayerWornItems.Hands, "Hands");
                AddToInventoryList(iInventory.PlayerWornItems.Back, "Back");
                AddToInventoryList(iInventory.PlayerWornItems.Neck, "Neck");
                AddToInventoryList(iInventory.PlayerWornItems.Torso, "Torso");
                AddToInventoryList(iInventory.PlayerWornItems.Ears, "Ears");
                AddToInventoryList(iInventory.PlayerWornItems.Arms, "Arms");
                AddToInventoryList(iInventory.PlayerWornItems.Feet, "Feet");
                AddToInventoryList(iInventory.PlayerWornItems.FingerOne, "Finger One");
                AddToInventoryList(iInventory.PlayerWornItems.FingerTwo, "Finger Two");
                AddToInventoryList(iInventory.PlayerWornItems.Legs, "Legs");
                AddToInventoryList(iInventory.PlayerWornItems.OffHand, "Off-Hand");
                AddToInventoryList(iInventory.PlayerWornItems.Waist, "Waist");
                AddToInventoryList(iInventory.PlayerWornItems.WristOne, "Wrist");
                AddToInventoryList(iInventory.PlayerWornItems.WristTwo, "Wrist");
                AddToInventoryList(iInventory.PlayerWornItems.Worn, "Worn");
                AddToInventoryList(iInventory.PlayerWornItems.Readied, "Readied");

                foreach (string item in iInventory.UnEquippedItems)
                {
                    AddToUnEquippedList(item);
                }
                foreach (string key in iInventory.Keys)
                {
                    AddToKeyList(key);
                }
            });

        }
    }
}
